<template>
  <!-- 精彩评论页 -->
  <div class="comments">
    <hmheader page="精彩评论">
      <span
        slot="left"
        class="iconfont iconjiantou2"
        @click="$router.back()"
      ></span>
    </hmheader>
    <div class="lists" v-for="item in compiled" :key="item.id">
      <div class="item">
        <div class="head">
          <img :src="item.user.head_img" alt />
          <div>
            <p>{{ item.user.nickname }}</p>
            <span>{{ item.create_date | relative }}</span>
          </div>
          <span @click="reply(item)">回复</span>
        </div>
        <mycomments
          v-if="item.parent"
          :parent="item.parent"
          @parentes="reply"
        ></mycomments>
        <div class="text">{{ item.content }}</div>
      </div>
    </div>
    <myarticles
      :article="parent"
      @refresh="refresh"
      :contents="temp"
      @resr="temp = null"
    ></myarticles>
  </div>
</template>

<script>
import hmheader from "@/components/header.vue";
import myarticles from "@/components/myarticles.vue";
import { post_comment } from "@/apis/user.js";
import axios from "@/utils/myaxios.js";
import { relative } from "@/utils/time_filter.js";
import mycomments from "@/components/mycomments.vue";
import { postId } from "@/apis/post.js";
export default {
  components: {
    hmheader,
    myarticles,
    mycomments,
  },
  filters: {
    relative,
  },
  data() {
    return {
      compiled: [],
      parent: {},
      temp: {},
    };
  },
  activated() {
    this.init();
  },
  methods: {
    reply(value) {
      this.temp = value;
    },
    async init() {
      let res = await post_comment(this.$route.params.id);
      this.compiled = res.data.data.map((v) => {
        v.user.head_img = axios.defaults.baseURL + v.user.head_img;
        return v;
      });
      let res2 = await postId(this.$route.params.id);
      this.parent = res2.data.data;
      console.log(res2);
    },
    refresh() {
      this.init();
      window.scrollTo(0, 0);
    },
  },
};
</script>

<style lang='less' scoped>
.comments {
  padding-bottom: 60px;
}

.lists {
  padding: 0 15px;
  border-top: 1px solid #ccc;
  .item {
    padding: 10px 0;

    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      > img {
        width: 50/360 * 100vw;
        height: 50/360 * 100vw;
        display: block;
        border-radius: 50%;
      }
      > div {
        flex: 1;
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        > span {
          font-size: 12px;
          color: #999;
          line-height: 25px;
        }
      }
      > span {
        color: #999;
        font-size: 13px;
      }
    }
    .text {
      font-size: 14px;
      color: #333;
      padding: 20px 0 10px 0;
    }
  }
}
</style>